﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using CircleCut.Models;
using Common;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CircleCut.Controllers
{ 
    public class InputsController : Controller
    {
        private InputDBContext db = new InputDBContext();
        public static List<CircleCut.Models.Input> InList = new List<CircleCut.Models.Input>();

        //public static List<Common.Models.Input> In = new List<Common.Models.Input>();
        //public static List<Common.Models.Output> Out = new List<Common.Models.Output>();

        /*
        void SGHCalc(int patternType)
        {
            
            if (In.Count < 1)
            {
                MessageBox.Show("没有输入数据！");
                return;
            }

            var paras = "";
            var task = Task.Run(() =>
            {

                Parallel.For(0, In.Count, i => new SGH.Program().Start(In[i], patternType, ref Out,paras)
                );
            });
            task.ContinueWith(o =>
            {
                MessageBox.Show("sgh计算完成");
            });
           
        }
        */


        // GET: Inputs
        public ActionResult Index(string searchNumber, string searchDiameter)
        {
            var NumLst = new List<string>();
            var NumQry = from d in db.Inputs
                         orderby d.Number
                         select d.Number;

            NumLst.AddRange(NumQry.Distinct());
            ViewBag.searchNumber = new SelectList(NumLst);

            var Num = from m in db.Inputs
                      select m;

            /*
            if (!String.IsNullOrEmpty(searchDiameter))
            {
                Inputs = Inputs.Where(s => s.Diameter.Contains(searchDiameter));
            }
            */

            if (!string.IsNullOrEmpty(searchNumber))
            {
                Num = Num.Where(x => x.Number == searchNumber);
            }

            InList = Num.ToList();
            /////////////////////////////
            /*
            var list = Num.ToList();
            var k = list[0];
            var h = k.Length;
            var i = k.Width;
            var j = k.DiaTotalNum;
            input.Length = double.Parse(h);
            input.Width = double.Parse(i);
            input.Num = int.Parse(j);
            */

            return View(Num);

        }

        // GET: Inputs/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Input input = db.Inputs.Find(id);
            if (input == null)
            {
                return HttpNotFound();
            }
            return View(input);
        }

        // GET: Inputs/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Inputs/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Number,Length,Width,DiaTotalNum")] Input input)
        {

            /*
            // 输入导入SGH
            string a = "[2000, 1000, 4]\r\n[350, 200]\r\n[250, 150]\r\n[220, 300]\r\n[330, 250]"
            ReadInput.ReadCalc(a, ref In);
            SGHCalc(0);
            */

            if (ModelState.IsValid)
            {
                db.Inputs.Add(input);
                db.SaveChanges();

                return RedirectToAction("Index");
               
            }            
            return View(input);           
    }


        // GET: Inputs/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Input input = db.Inputs.Find(id);
            if (input == null)
            {
                return HttpNotFound();
            }
            return View(input);
        }

        // POST: Inputs/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Number,Length,Width,DiaTotalNum")] Input input)
        {
            if (ModelState.IsValid)
            {
                db.Entry(input).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(input);
        }

        // GET: Inputs/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Input input = db.Inputs.Find(id);
            if (input == null)
            {
                return HttpNotFound();
            }
            return View(input);
        }

        // POST: Inputs/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Input input = db.Inputs.Find(id);
            db.Inputs.Remove(input);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
